package com.msi.androidrss;

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener; 
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.EventLogTags.Description;

import java.util.ArrayList;
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;

import org.xml.sax.XMLReader;

import android.content.Context;
import android.content.Intent;

import com.msi.androidrss.ShowDescription;

public class RSSReader//implements OnItemClickListener
{
	public final String STANDARD_MESSAGE= "Howard County public schools are operating on a normal schedule. Default message for Howard County Public Schools";
	public final String RSSFEEDOFCHOICE = "http://www.schoolsout.com/view/feed/47";
	private Context ontext;
	public final String tag = "RSSReader";
	private RSSFeed feed = null;
	
	/** Called when the activity is first created. */

    public RSSReader (Context ctxt){
        //super.onCreate(icicle);
        //ctxt.setHasOptionsMenu(true);
        ontext=ctxt;
        // go get our feed!
        feed = getFeed(RSSFEEDOFCHOICE);

        
        // display UI

    }

    public TextView doWeHaveSchool() {
    	TextView Discription=new TextView(ontext);
		if (feed == null) {
			Discription.setText("No RSS Feed Available...(no internet maybe?) Look elsewhere for the status of the schedule. :( ");
			return Discription;
		}
		String string = feed.getItem(0).getDescription()
				+ feed.getItem(0).getTitle();
		
		
		
		Discription.setText(string);
		if(Discription.getText()=="")Log.e("Problem", "Text is empty");
		else Log.e("It did print ", (String) Discription.getText());
		if(Discription.getText().equals(STANDARD_MESSAGE)) Discription.setTextColor(90);
		else if (((String) Discription.getText()).contains("closed")) Discription.setTextColor(900);
        return Discription;
    }
    
    private RSSFeed getFeed(String urlToRssFeed)
    {
    	try
    	{
    		// setup the url
    	   URL url = new URL(urlToRssFeed);

           // create the factory
           SAXParserFactory factory = SAXParserFactory.newInstance();
           // create a parser
           SAXParser parser = factory.newSAXParser();

           // create the reader (scanner)
           XMLReader xmlreader = parser.getXMLReader();
           // instantiate our handler
           RSSHandler theRssHandler = new RSSHandler();
           // assign our handler
           xmlreader.setContentHandler(theRssHandler);
           // get our data via the url class
           InputSource is = new InputSource(url.openStream());
           // perform the synchronous parse           
           xmlreader.parse(is);
           // get the results - should be a fully populated RSSFeed instance, or null on error
           return theRssHandler.getFeed();
    	}
    	catch (Exception ee)
    	{
    		ee.printStackTrace();
    		System.exit(45);
    		
    		Log.v("error",ee.toString());
    		// if we have a problem, simply return null
    		return null;
    	}
    }
    //TODO add this back in
//    public boolean onCreateOptionsMenu(Menu menu) 
//    {
//    	
//    	super.onCreateOptionsMenu(menu, null);
//    	
//    	menu.add(0,0,0, "Choose RSS Feed");
//    	menu.add(0,1,1, "Refresh");
//    	Log.i(tag,"onCreateOptionsMenu");
//    	return true;
//    }
//    
//    public boolean onOptionsItemSelected(MenuItem item){
//        switch (item.getItemId()) {
//        case 0:
//        	
//        	Log.i(tag,"Set RSS Feed");
//            return true;
//        case 1:
//        	Log.i(tag,"Refreshing RSS Feed");
//            return true;
//        }
//        return false;
//    }
    
    
//    private void UpdateDisplay()
//    {
//    	Log.e("Got here", "Got here");
//    	final Context thisContext= this;
//        Runnable runnerRunnable= new Runnable() {
//			public void run() {
//				Log.e("Got here", "Got here too");
//			//	TextView Discription = (TextView) findViewById(R.id.discription);
//				//TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
//				TextView Discription=new TextView(thisContext);
//				if (feed == null) {
//					Discription.setText("No RSS Feed Available");
//					return;
//				}
//				String string = feed.getItem(0).getDescription()
//						+ feed.getItem(0).getTitle();
//				Discription.setText(string);
//				if(Discription.getText()=="")Log.e("Problem", "Text is empty");
//				else Log.e("It did print ", (String) Discription.getText());
//				Discription.setText("hi!");
//				setContentView(Discription);
//				//feedpubdate.setText((String)feed.getPubDate());
//				//
//				//        ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems());
//				//
//				//        itemlist.setAdapter(adapter);
//				//        
//				//        itemlist.setOnItemClickListener(this);
//				//        
//				//        itemlist.setSelection(0);
//			}
//		};	
//		runOnUiThread(runnerRunnable);
//        TextView testView= (TextView)findViewById(R.id.discription);
//        testView.setText("Ughh");
//    }
//    
//    
//     public void onItemClick(AdapterView parent, View v, int position, long id)
//     {
//    	 Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");
//
//    	 Intent itemintent = new Intent(this,ShowDescription.class);
//         
//    	 Bundle b = new Bundle();
//    	 b.putString("title", feed.getItem(position).getTitle());
//    	 b.putString("description", feed.getItem(position).getDescription());
//    	 b.putString("link", feed.getItem(position).getLink());
//    	 b.putString("pubdate", feed.getItem(position).getPubDate());
//    	 
//    	 itemintent.putExtra("android.intent.extra.INTENT", b);
//         
//         startActivityForResult(itemintent, 0);
//     }
    
}